import { MenuDataItem } from "@ant-design/pro-components";
import { CrownOutlined } from "@ant-design/icons";
import ACCESS_ENUM from "@/access/accessEnum";

export const menus: MenuDataItem[] = [
  {
    name: "首页",
    path: "/",
  },
  {
    name: "题目",
    path: "/questions",
  },
  {
    name: "题库",
    path: "/banks",
  },
  {
    name: "管理",
    path: "/admin",
    icon: <CrownOutlined />,
    access: ACCESS_ENUM.ADMIN,
    children: [
      {
        name: "用户管理",
        path: "/admin/users",
        access: ACCESS_ENUM.ADMIN,
      },
      {
        name: "题库管理",
        path: "/admin/banks",
        access: ACCESS_ENUM.ADMIN,
      },
      {
        name: "题目管理",
        path: "/admin/questions",
        access: ACCESS_ENUM.ADMIN,
      },
    ],
  },
];

/**
 * 根据全部路径获取菜单数据
 * @param path
 * @returns
 */
export const findAllMenuItemByPath = (path: string): MenuDataItem | null => {
  return findMenuItemByPath(menus, path);
};
/**
 * 根据路径获取菜单数据（递归）
 * @param path
 * @returns
 */
export const findMenuItemByPath = (
  menus: MenuDataItem[],
  path: string
): MenuDataItem | null => {
  for (const menu of menus) {
    if (menu.path === path) {
      return menu;
    }
    if (menu.children) {
      const child = findMenuItemByPath(menu.children, path);
      if (child) {
        return child;
      }
    }
  }
  return null;
};
